package controller

import (
	"encoding/json"
	"miao/dto"
	"miao/service"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func UserPage(c *gin.Context) {
	currentPage := c.DefaultQuery("currentPage", "1")
	pageSize := c.DefaultQuery("pageSize", "10")
	username := c.Query("username")
	intCurrentPage, _ := strconv.Atoi(currentPage)
	intPageSize, _ := strconv.Atoi(pageSize)
	result := service.Page(intCurrentPage, intPageSize, username)
	c.JSON(http.StatusOK, result)
}

func UserDelete(c *gin.Context) {
	userId := c.Param("userId")
	result := service.DeleteUser(userId)
	if result.Code != 200 {
		c.JSON(http.StatusInternalServerError, result)
		return
	}
	c.JSON(http.StatusOK, result)
}

func SaveUser(c *gin.Context) {
	b, _ := c.GetRawData()
	user := dto.UserDto{}
	_ = json.Unmarshal(b, &user)
	result := service.SaveUser(&user)
	c.JSON(http.StatusOK, result)
}
